/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package org.whuims.service.news;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;
import org.whuims.entity.Infor;
import org.whuims.repository.jpa.InforDao;

import java.util.List;
import java.util.Map;

// Spring Bean的标识.
@Component
// 类中所有public函数都纳入事务管理的标识.
@Transactional
public class InforService {

    private InforDao inforDao;

    public Infor getInfor(Long id) {
        return inforDao.findOne(id);
    }

    public void saveInfor(Infor entity) {
        inforDao.save(entity);
    }

    public void deleteInfor(Long id) {
        inforDao.delete(id);
    }

    public List<Infor> getAllInfors() {
        return (List<Infor>) inforDao.findAll();
    }

    public List<Infor> findTopInfors(int size) {
        Sort sort = new Sort(Direction.DESC, "id");
        PageRequest pr = new PageRequest(0, size, sort);
        return inforDao.findAll(pr).getContent();
    }

    public Page<Infor> getTypeInfors(Integer type, Map<String, Object> searchParams, int pageNumber, int pageSize,
                                     String sortType) {
        PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortType);
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        if (type != null) {
            filters.put("type", new SearchFilter("type", Operator.EQ, type));
        }
        Specification<Infor> spec = DynamicSpecifications.bySearchFilter(filters.values(), Infor.class);
        return inforDao.findAll(spec, pageRequest);
    }

    public Page<Infor> getUserInfors(Long userId, Map<String, Object> searchParams, int pageNumber, int pageSize,
                                     String sortType) {
        PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortType);
        Specification<Infor> spec = buildSpecification(userId, searchParams);
        return inforDao.findAll(spec, pageRequest);
    }

    public Page<Infor> getInforList(Map<String, Object> searchParams, int pageNumber, int pageSize,
                                    String sortType) {
        PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortType);
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        Specification<Infor> spec = DynamicSpecifications.bySearchFilter(filters.values(), Infor.class);
        return inforDao.findAll(spec, pageRequest);
    }

    /**
     * 创建分页请求.
     */
    private PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortType) {
        Sort sort = null;
        if (sortType == null) sortType = "auto";
        if ("auto".equals(sortType)) {
            sort = new Sort(Direction.DESC, "id");
        } else if ("title".equals(sortType)) {
            sort = new Sort(Direction.ASC, "title");
        }
        return new PageRequest(pageNumber - 1, pagzSize, sort);
    }

    /**
     * 创建动态查询条件组合.
     */
    private Specification<Infor> buildSpecification(Long userId, Map<String, Object> searchParams) {
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        filters.put("author.id", new SearchFilter("author.id", Operator.EQ, userId));
        Specification<Infor> spec = DynamicSpecifications.bySearchFilter(filters.values(), Infor.class);
        return spec;
    }

    @Autowired
    public void setInforDao(InforDao inforDao) {
        this.inforDao = inforDao;
    }
}
